package ua.javatr.android.smb;

import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Вячеслав on 11.06.2014.
 */
public class CardListFragment extends ListFragment {

    String[] cards = {"0123456", "234567", "345678", "456789"};
    String[] names = {"Card1", "Card2", "Card3", "Card4"};
    Integer[] pics = {R.drawable.ic_launcher, R.drawable.oshad,
            R.drawable.privatbank, R.drawable.pumb};

    OnCardSelectedListener onCardSelected;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        onCardSelected = (OnCardSelectedListener) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < cards.length; i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("cardname", names[i]);
            hm.put("cardnumber", cards[i]);
            hm.put("logo", String.valueOf(pics[i]));
            list.add(hm);
        }
        String[] from = {"cardname", "cardnumber", "logo"};
        int[] to = {R.id.tvCardName, R.id.tvCardNumber, R.id.ivBankLogo};

        SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.fragment_list_custom_layout, from, to);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        onCardSelected.onCardSelected(l.getItemAtPosition(position).toString());
    }

    public interface OnCardSelectedListener {
        void onCardSelected(String cardNumber);
    }
}
